<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="50%">
      <el-form
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm form"
        label-width="100px"
        :model="form"
        v-loading="loading"
      >
        <el-form-item class="item" label="线缆类型:" prop="cableClass">
          <el-select
            style="width: 100%"
            v-model="form.cableClass"
            filterable
            placeholder="请选择线缆类型"
            @change="handChange"
          >
            <el-option
              v-for="item in cableDate"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="item" label="测量长度:" prop="measuringLength">
          <el-input
            v-model="form.measuringLength"
            placeholder="请输入测量长度"
          ></el-input>
        </el-form-item>
        <el-form-item class="item" label="规格型号:" prop="cableModel">
          <el-select
            style="width: 100%"
            v-model="form.cableModel"
            filterable
            placeholder="请选择规格型号"
          >
            <el-option
              v-for="item in modelDate"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item class="item" label="录入时间:" prop="createTime">
          <el-date-picker
            v-model="createTime"
            type="datetime"
            placeholder="选择生产日期"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item> -->
        <el-form-item class="item" label="估算长度:" prop="estimatedLength">
          <el-input
            style="width: 100%"
            v-model="form.estimatedLength"
            placeholder="请输入估算长度"
          ></el-input>
        </el-form-item>
        <el-form-item class="item" label="重量:" prop="weight">
          <el-input
            style="width: 100%"
            v-model="form.weight"
            placeholder="请输入重量"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" class="loginBth2">取 消</el-button>
        <el-button
          type="primary"
          @click="submit"
          :loading="loading"
          class="loginBth"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addWeighingRecord,
  getWeightDetail,
  editWeightDetail,
  getClassPullDown,
  getModelByclass,
} from "@/common/api";
import { formatISODate } from "@/untils/trans";
export default {
  name: "AddWeight",
  props: {},
  components: {},
  computed: {
    title() {
      return this.type == "add" ? "新增数据" : "编辑数据";
    },
  },

  data() {
    return {
      dialogFormVisible: false,
      options: [],
      form: {
        cableClass: "",
        cableModel: "",
        estimatedLength: "",
        weight: "",
        measuringLength: "",
      },
      //   createTime: "",
      rules: {
        cableClass: [
          { required: true, message: "请选择线缆类型", trigger: "blur" },
        ],
        cableModel: [
          { required: true, message: "请选择规格型号", trigger: "blur" },
        ],
      },
      loading: false,
      type: "add",
      id: "",
      cableDate: [], //线缆类型
      modelDate: [], // 规格型号
    };
  },
  mounted() {},
  watch: {
    "form.cableClass": function (newVal) {
      if (!newVal) return;
      this.getModelByclass(newVal);
    },
  },
  created() {
    this.getClassPullDown();
  },
  methods: {
    handChange() {
      this.form.cableModel = "";
    },
    getClassPullDown() {
      getClassPullDown().then((res) => {
        if (res.success) {
          this.cableDate = res.data;
        } else {
          this.$errorMsg(res.message);
        }
      });
    },
    getModelByclass(newVal) {
      getModelByclass({ cableClass: newVal }).then((res) => {
        if (res.success) {
          this.modelDate = res.data;
        } else {
          this.$errorMsg(res.message);
        }
      });
    },

    getDetail(id) {
      this.$refs["ruleForm"]?.resetFields();
      this.loading = true;
      this.id = id;
      getWeightDetail({ id })
        .then((res) => {
          this.loading = false;
          if (res.success) {
            for (let key in this.form) {
              this.form[key] = res.data[key];
              //   this.createTime = res.data.createTime;
            }
          } else {
            this.$errorMsg(res.message);
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
    submit() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.type == "add") {
            let param = {
              ...this.form,
              //   createTime: this.createTime
              //     ? formatISODate(this.createTime)
              //     : this.createTime,
            };

            addWeighingRecord(param)
              .then((res) => {
                this.loading = false;
                if (res.success) {
                  this.$successMsg("添加成功");
                  this.dialogFormVisible = false;
                  //   this.createTime = "";
                  let arr = Object.keys(this.form);
                  this.$emit("getTable");
                  arr.forEach((i) => (this.form[i] = "")); //表单数据置空
                } else {
                  this.$errorMsg(res.message);
                }
              })
              .catch((err) => {
                this.loading = false;
              });
          } else {
            editWeightDetail({
              ...this.form,
              id: this.id,
              //   createTime: this.createTime
              //     ? formatISODate(this.createTime)
              //     : this.createTime,
            })
              .then((res) => {
                this.loading = false;
                if (res.success) {
                  this.$successMsg("修改成功");
                  this.dialogFormVisible = false;
                  let arr = Object.keys(this.form);
                  arr.forEach((i) => (this.form[i] = "")); //表单数据置空
                  //   this.createTime = "";
                  this.$emit("getTable");
                } else {
                  this.$errorMsg(res.message);
                }
              })
              .catch((err) => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.dialogFormVisible = false;
      let arr = Object.keys(this.form);
      arr.forEach((i) => (this.form[i] = "")); //表单数据置空
      //   this.createTime = "";
    },
  },
};
</script>


<style lang="scss" scoped>
.form {
  display: flex;
  //   justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  //   gap: 20px;
  .item {
    width: 50%;
  }
}
</style>